package com.huiguo.methodargument.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;

@Data
@AllArgsConstructor
public class InvokeResult<T> implements Serializable {
    private String status;
    private String message;
    private T data;

    private final static String STATUS_SUCCESS = "200";
    private final static String MESSAGE_SUCCESS = "SUCCESS";
    private final static String MESSAGE_FAILURE = "FAILUER";

    //参数校验失败异常
    public final static String TYPE_FAIL_PARAM = "-1";

    public InvokeResult(T data, String type) {
        if (!StringUtils.isEmpty(type) && !type.equals(STATUS_SUCCESS)) {
            this.status = type;
            this.message = MESSAGE_FAILURE;
            this.data = data;
        } else {
            this.status = STATUS_SUCCESS;
            this.message = MESSAGE_SUCCESS;
            this.data = data;
        }

    }

    public InvokeResult(T data) {
        this.status = STATUS_SUCCESS;
        this.message = MESSAGE_SUCCESS;
        this.data = data;
    }

    public InvokeResult() {
        this(null);
    }

    public static <T> InvokeResult<T> success() {
        return new InvokeResult<>();
    }

    public static <T> InvokeResult<T> success(T data) {
        return new InvokeResult<>(data);
    }

    public static InvokeResult<String> error(String errorMessage, String type) {
        return new InvokeResult<>(errorMessage, type);
    }


}